package cn.jbolt.util;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import cn.jbolt.config.common.ResultSerializer;
import org.springframework.stereotype.Component;

/**
 * 返回类
 */
@Component
@JsonSerialize(using = ResultSerializer.class)
public class Result<T> {

    // 定义code编码
    public final static int SUCCESS_CODE = 0;
    public final static int ERROR_CODE = 500;
    /**
     * 向量数据库无匹配
     */
    public static final int ERROR_VDB_NOMATCH = 501;

    // 定义state返回的状态
    public final static String SUCCESS_STATE = "ok";
    public final static String ERROR_STATE = "error";

    private int code;
    private T data; // 使用泛型以支持任意类型的列表
    private String state;
    private String msg;

    /**
     * 私有构造函数，防止直接实例化
     */
    public Result() {}


    /**
     * 判断是否为成功状态
     * @return true 如果状态是成功，否则 false
     */
    public boolean isSuccess() {
        return SUCCESS_STATE.equals(state);
    }

    /**
     * 判断是否为错误状态
     * @return true 如果状态是错误，否则 false
     */
    public boolean isError() {
        return ERROR_STATE.equals(state);
    }


    /**
     * 成功的构造方法
     * @param code
     * @param data
     * @param state
     * @param msg
     */
    private Result(int code, T data, String state, String msg) {
        this.code = code;
        this.data = data;
        this.state = state;
        this.msg = msg;
    }

    /**
     * 失败的构造方法
     * @param code
     * @param state
     * @param msg
     */
    private Result(int code, String state, String msg) {
        this.code = code;
        this.data = null;
        this.state = state;
        this.msg = msg;
    }

    /**
     * 静态工厂方法 - 创建成功响应,不带消息
     * @param
     * @return
     * @param <T>
     */
    public static <T> Result<T> success() {
        return new Result<>(SUCCESS_CODE, null, SUCCESS_STATE, null);
    }

    /**
     * 静态工厂方法 - 创建成功响应
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, data, SUCCESS_STATE, null);
    }

    /**
     * 静态工厂方法 - 创建成功响应（带消息）
     * @param data
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data, String msg) {
        return new Result<>(SUCCESS_CODE, data, SUCCESS_STATE, msg);
    }

    /**
     * 静态工厂方法 - 创建失败响应
     * @param msg
     * @return
     */
    public static Result error(String msg) {
        return new Result(ERROR_CODE, ERROR_STATE, msg);
    }



    /**
     * 静态工厂方法 - 创建自定义错误码的失败响应
     * @param code
     * @param msg
     * @return
     */
    public static Result error(int code, String msg) {
        return new Result(code, ERROR_STATE, msg);
    }

    // Getter 方法
    public int getCode() {
        return code;
    }

    public T getData() {
        return data;
    }

    public String getState() {
        return state;
    }

    public String getMsg() {
        return msg;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setState(String state) {
        this.state = state;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
